home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 April: Mac OS SDK / Dev.CD Apr 00 SDK1.toast / Development Kits / Mac OS / Thread Manager / ThreadUtilities / SnakesWithSemaphores / ThreadedSnakesMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-17  |  5.3 KB  |  253 lines  |  [TEXT/MPS ]

  1. /*
  2.  *  ThreadedSnakesMain.c
  3.  *
  4.  *  Author:  Brad Post
  5.  *    Creation Date:    12/16/92
  6.  *  Copyright  © 1992, 1993  Apple Computer Inc.
  7.  *
  8.  *     This file is the main for ThreadedSnakes.  It just creates the window, handles the user events and 
  9.  *  simple stuff.
  10.  */
  11.  
  12. #include "ThreadedSnakes.h"
  13.  
  14. VEC                    theBounds;            // bounds to which the snakes can be draw into
  15. MenuHandle            myMenus[2];            // menu handles
  16. WindowPtr            theWindow;            // window to draw the snakes in            
  17. WindowRecord        theWindowRec;        // windowrec for the window we draw snakes in
  18.  
  19. /*
  20.  *  myError
  21.  *
  22.  *  This function just takes in a Str255 and pops up an ALRT box saying what's wrong, and then punts
  23.  *  away.
  24.  */
  25. myError(Str255 theString)
  26. {
  27.     ParamText(theString, NULL, NULL, NULL);
  28.     Alert(errorALRT, NULL);
  29.     ExitToShell();
  30. }
  31.  
  32. /* 
  33.  *    InitAll
  34.  *
  35.  *  Basic Initialization routine.  Takes care of creating the window to draw the snakes in as well as setting up
  36.  *  the menus.
  37.  */
  38. InitAll()
  39. {    
  40.     Rect theRect;
  41.     
  42.     MaxApplZone();
  43.     InitGraf(&qd.thePort);
  44.     InitFonts();
  45.     FlushEvents(everyEvent, 0);
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(0L);
  50.     InitCursor();
  51.     
  52.     /*  Set up the menus. */
  53.     myMenus[1] = GetMenu(fileMenuID);
  54.     InsertMenu(myMenus[0] = NewMenu(1, "\p\024"), 0);
  55.     InsertMenu(myMenus[1], 0);
  56.     AppendMenu(myMenus[0], "\pAbout SnakesWSemaphores;(-");
  57.     AddResMenu(myMenus[0], 'DRVR');
  58.     DrawMenuBar();
  59.  
  60.     /* Determine the size of the screen */
  61.     theRect = qd.screenBits.bounds;
  62.     
  63.     theRect.top += (2 * MBarHeight);
  64.     theRect.left += (2 * MBarHeight);
  65.     theRect.right -= (2 * MBarHeight);
  66.     theRect.bottom -= (2 * MBarHeight);
  67.     
  68.     /* Create a window to draw the snakes in, bounded by the rectangle 'theRect'. */
  69.     
  70.     theWindow = NewWindow(&(theWindowRec), &theRect, "\p", 1, noGrowDocProc, (WindowPtr) -1, 0, 0);
  71.     ShowWindow(theWindow);
  72.     SetPort(theWindow);
  73.  
  74.     /*  To determine where the snake can move to, set these bounds. */
  75.     theBounds[0] = theRect.bottom;     // for testing the V of a point
  76.     theBounds[1] = theRect.right;     // for testing the H of a point
  77.  
  78.     /* Make the necessary initialization calls */
  79.     InitializeThreadUtilities();
  80.     initSnakes();
  81.     
  82. }
  83.  
  84.  
  85. /*
  86.  *    IsAppWindow
  87.  *
  88.  *  Returns true is the window is the app window, otherwise false.
  89.  */
  90. Boolean IsAppWindow(window)
  91.     WindowPtr    window;
  92. {
  93.     short        windowKind;
  94.  
  95.     if ( window == nil )
  96.         return false;
  97.     else {    /* application windows have windowKinds = userKind (8) */
  98.         windowKind = ((WindowPeek) window)->windowKind;
  99.         return ( windowKind == userKind );
  100.     }
  101. }
  102.  
  103. /*
  104.  *    DoUpdate
  105.  *
  106.  *  Handles update events.
  107.  */
  108. void DoUpdate(window)
  109.     WindowPtr    window;
  110. {
  111.     if ( IsAppWindow(window) ) {
  112.         BeginUpdate(window);                /* this sets up the visRgn */
  113.         if ( ! EmptyRgn(window->visRgn) )    /* draw if updating needs to be done */
  114.             ;
  115.         EndUpdate(window);
  116.     }
  117. }
  118.  
  119. /*
  120.  *    DoActivate
  121.  *
  122.  *  Handles activate events.
  123.  */
  124. void DoActivate(window, becomingActive)
  125.     WindowPtr    window;
  126.     Boolean        becomingActive;
  127. {
  128.     if ( IsAppWindow(window) ) {
  129.         if ( becomingActive )
  130.             /* do whatever you need to at activation */ ;
  131.         else
  132.             /* do whatever you need to at deactivation */ ;
  133.     }
  134.  
  135. /*
  136.  *    doMenus
  137.  *
  138.  *  This function handles all the menu stuff.
  139.  */
  140. short doMenus(long selector)
  141. {
  142.     short     whichMenu = HiWord(selector);
  143.     short     whichItem = LoWord(selector);
  144.     GrafPtr    savePort;
  145.     Str255    name;
  146.     
  147.     switch(whichMenu)
  148.     {
  149.         case appleMenuID:
  150.             switch(whichItem)
  151.             {
  152.                 case 1:    Alert(aboutALRT, 0L);
  153.                         return notDONE;
  154.                 default:
  155.                 {
  156.                     GetPort(&savePort);
  157.                     GetItem(myMenus[0], whichItem, name);
  158.                     OpenDeskAcc(name);
  159.                     SetPort(savePort);
  160.                     return notDONE;
  161.                 }
  162.             }
  163.             break;                
  164.         case fileMenuID:
  165.             switch(whichItem)
  166.             {
  167.                 default:
  168.                     return notDONE;
  169.                 case fmQuit:
  170.                     cleanUpSnakes();
  171.                     ExitToShell();
  172.                     return areDONE;
  173.             }
  174.             break;
  175.     }
  176.     
  177.     HiliteMenu(0);
  178.     return notDONE;
  179.  
  180. }    
  181.  
  182.  
  183. /*
  184.  *    main
  185.  *
  186.  *  The main function that just handles the event loop and yeilds to any thread.
  187.  *  The event loop is in a critical section so that no preemptive thread (which are using quickdraw)
  188.  *  can interrupt us and draw through out menu bar and such.
  189.  */
  190. main()
  191. {
  192.     short        done = 0;
  193.     short        whichWindow;
  194.     WindowPtr    theWindow;
  195.     EventRecord    myEvent;
  196.     Point        aPoint;
  197.     
  198.     InitAll();
  199.     
  200.     while( !done )
  201.     {
  202.         ThreadBeginCritical();
  203.         
  204.         SystemTask();
  205.         if(GetNextEvent(everyEvent, &myEvent) == true)
  206.         {
  207.             switch(myEvent.what)
  208.             {
  209.                 case mouseDown: 
  210.                 {
  211.                     whichWindow = FindWindow(myEvent.where, &theWindow);
  212.                     switch(whichWindow)
  213.                     {
  214.                         case inSysWindow:
  215.                             SystemClick(&myEvent, theWindow);
  216.                             break;
  217.                         case inMenuBar:
  218.                             done = doMenus(MenuSelect(myEvent.where));
  219.                             break;
  220.                         case inDrag:
  221.                             DragWindow( theWindow, myEvent.where, &qd.screenBits.bounds);
  222.                             break;
  223.                     }
  224.                 } 
  225.                     break;
  226.                 case keyDown:
  227.                 case autoKey:
  228.                     if((myEvent.modifiers & cmdKey) != 0)
  229.                         done = doMenus(MenuKey( (char) (myEvent.message & charCodeMask)));
  230.                     break;
  231.                 case activateEvt:
  232.                     DoActivate((WindowPtr) myEvent.message, (myEvent.modifiers & activeFlag) != 0);
  233.                     break;
  234.                 case updateEvt:
  235.                     DoUpdate((WindowPtr) myEvent.message);
  236.                     break;
  237.                 case diskEvt:
  238.                     if ( HiWord(myEvent.message) != noErr ) {
  239.                         SetPt(&aPoint, kDILeft, kDITop);
  240.                         DIBadMount(aPoint, myEvent.message);
  241.                     }
  242.                     break;
  243.             }
  244.         }
  245.         
  246.         ThreadEndCritical();
  247.                 
  248.         if(YieldToAnyThread() != noErr)
  249.             DebugStr("\pGot an error on YieldToAnyThread in Main Thread");    
  250.     }
  251. }
  252.